﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CuteEditor;
using Wjtour.Domain;
using Wjtour.BLL;
using Hjp.FrameWork.Common;
using System.IO;

namespace Wjtour.Web.Admin.scene
{
    public partial class addedi : BasePage
    {
        protected DropDownList scenerank;
        protected TextBox scenename;
        protected TextBox opentime;
        protected TextBox address;
        protected TextBox contactphone;
        protected TextBox sceneimpression;
        protected TextBox sceneweb;
        protected TextBox price;
        protected DropDownList scenetype;
        protected Editor trafficguide;
        protected Editor introduce;
        protected Button submit, submit2;
        protected string type;
        protected CheckBox isrecommend;
        protected CuteWebUI.Uploader Uploader1;
        protected TextBox img;
        protected FileUpload upload;
        protected Image imgPreview;
        protected Button btnPublish;
        protected Button button1;

        protected int id = 0;
        protected int categoryid = 0;
        Wjtour.Domain.scene entity = new Domain.scene();
        scene_bll _scene_bll = new scene_bll();
        scenepicture_bll _scenepicture_bll = new scenepicture_bll();
        private List<Wjtour.Domain.dto.sitepicture_picturetype> tempPictures = new List<Wjtour.Domain.dto.sitepicture_picturetype>();

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Utils.StrToInt(Request["id"], 0);
            categoryid = Utils.StrToInt(Request["cid"], 0);
            introduce.AutoConfigure = AutoConfigure.Simple;
            trafficguide.AutoConfigure = AutoConfigure.Simple;
            type = Request["type"];
            submit.Click += new EventHandler(submit_Click);
            submit2.Click += new EventHandler(submit_Click);
            Uploader1.UploadCompleted += Uploader1_UploadCompleted;
            btnPublish.Click += btnPublish_Click;

            if (id > 0)
            {
                entity = _scene_bll.Load(id);
                if (!IsPostBack)
                {
                    if (entity != null)
                    {
                        scenename.Text = entity.scenename;
                        address.Text = entity.address;
                        scenerank.SelectedValue = entity.scenerank.ToString();
                        opentime.Text = entity.opentime;
                        contactphone.Text = entity.contactphone;
                        sceneimpression.Text = entity.sceneimpression.ToString();
                        sceneweb.Text = entity.sceneweb;
                        price.Text = entity.price.ToString();
                        scenetype.SelectedValue = entity.scenetype.ToString();
                        trafficguide.Text = entity.trafficguide;
                        introduce.Text = entity.introduce;
                        isrecommend.Checked = entity.isrecommend == 1;
                        img.Text = entity.img;
                        imgPreview.ImageUrl = entity.img;
                    }
                }
            }

        }

        void btnPublish_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(upload.FileName))
            {
                Utils.Alert(this.Page, "请选择要上传的文件！");
                return;
            }
            string fileExt = Path.GetExtension(upload.FileName);
            List<string> allowExt = new List<string>();
            allowExt.Add(".jpg");
            allowExt.Add(".png");
            allowExt.Add(".gif");

            if (!allowExt.Contains(fileExt))
            {
                Utils.Alert(this.Page, "系统只允许上传jpg,jpeg,gif的文件！");
                return;
            }
            string uploadDir = Server.MapPath("/uploads");
            if (!Directory.Exists(uploadDir))
            {
                Directory.CreateDirectory(uploadDir);
            }

            string guid = GetFileName("time");
            string filename = guid + fileExt;
            string path = uploadDir + "\\" + filename;
            if (System.IO.File.Exists(filename)) System.IO.File.Delete(path);

            try
            {
                upload.SaveAs(path);
                string newpath = ImageUtil.ConvertImageToProperSize(HttpContext.Current, "/uploads/" + filename, 152, 111, "jpg");
                imgPreview.ImageUrl = newpath;
                img.Text = newpath;
            }
            catch (Exception ex)
            {
                Utils.Alert(this.Page, "文件文件出错，文件大小超过限制或网络原因！");
                return;
            }
        }

        void Uploader1_UploadCompleted(object sender, CuteWebUI.UploaderEventArgs[] args)
        {
            args.ToList().ForEach(a =>
            {
                string filename = GetFileName("time");
                a.CopyTo("~/uploads/" + filename + ".jpg");
                a.Delete();
                var sp = new Wjtour.Domain.dto.sitepicture_picturetype
                {
                    picturepath = "/uploads/" + filename + ".jpg",
                    picturetitle = filename,
                    iscover = 0
                };
                var smallImg = ImageUtil.ConvertImageToProperSize(HttpContext.Current, "/uploads/" + filename + ".jpg", 156, 106, "jpg");
                sp.smallImg = smallImg;
                tempPictures.Add(sp);
                if (id > 0)
                {
                    var sitepic = new sitepicture();
                    sitepic.picturepath = sp.picturepath;
                    sitepic.picturetitle = sp.picturetitle;
                    sitepic.smallImg = sp.smallImg;
                    sitepic.createdate = DateTime.Now;
                    sitepic.iscover = 0;
                    sitepic.isactive = 1;
                    sitepic.isaudit = 0;
                    sitepic.zan = 0;
                    sitepic.picturetypeid = 6;

                    _scenepicture_bll.Save(sitepic, id);
                }
            });
            Utils.ExecScript(this.Page, "calldata('/scenepicture.ashx', 1, 1);");
        }

        void submit_Click(object sender, EventArgs e)
        {
            entity.id = id;
            entity.scenename = scenename.Text;
            entity.address = address.Text;
            entity.scenerank = Utils.StrToInt(scenerank.SelectedValue, 0);
            entity.opentime = opentime.Text;
            entity.contactphone = contactphone.Text;
            entity.sceneimpression = sceneimpression.Text;
            entity.sceneweb = sceneweb.Text;
            entity.price = Utils.StrToDouble(price.Text, 0);
            entity.scenetype = Utils.StrToInt(scenetype.SelectedValue, 0);
            entity.trafficguide = trafficguide.Text;
            entity.introduce = introduce.Text;
            entity.isrecommend = isrecommend.Checked ? 1 : 0;
            entity.img = imgPreview.ImageUrl;

            if (entity.id == 0)
                entity.addtime = DateTime.Now;

            bool b = _scene_bll.Save(entity);
            if (b)
            {
                Utils.Alert(this.Page, "保存成功！");
                Utils.Redirect(this.Page, "list.aspx", false);
            }
        }
    }
}